const userService = require("@/server/user");
const {
  sendData,
  successData,
  success,
  sendCode,
} = require("@/utils/r/result");
const gin_context = require("@/utils/gin_context");

const userAuthAPI = {
  //登录接口
  async login(req, res) {
    //取用户信息
    let userinfo = req.body;
    let { loginVo, code } = await userService.login(
      req,
      userinfo.username,
      userinfo.password
    );

    sendData(res, code, loginVo);
  },
  //获取用户详细信息
  async GetInfo(req, res) {
    //先获取上下文中中登录保存的用户id，根据id查询
    let result = await userService.getInfo(
      gin_context.GetFromContext(req, "user_info_id")
    );
    successData(res, result);
  },
  async Logout(req, res) {
    await userService.Logout(req);
    success(res);
  },
  async GetOnlineList(req, res) {
    successData(res, await userService.GetOnlineList(req.query));
  },
  async GetList(req, res) {
    successData(res, await userService.GetList(req.query));
  },
  async UpdateCurrent(req, res) {
    let currentUser = req.body;
    currentUser.id = gin_context.GetFromContext(req, "user_info_id");
    sendCode(res, await userService.UpdateCurrent(currentUser));
  },
  async UpdateCurrentPassword(req, res) {
    sendCode(
      res,
      await userService.UpdateCurrentPassword(
        req.body,
        gin_context.GetFromContext(req, "user_info_id")
      )
    );
  },
  async Register(req, res) {
    sendCode(res, await userService.Register(req.body));
  },
};

module.exports = userAuthAPI;
